//
//  MCTrainRecommandCell.swift
//  MCBodyBuildingPro
//
//  Created by Renlvshijie on 2023/5/31.
//

import UIKit
import ObjectMapper
import Kingfisher
import Toast_Swift


class MCTrainRecommandCell: UITableViewCell {

    @IBOutlet weak var recommandCollectionView: UICollectionView!
    
    let recommandCollectionIdentifier = "recommandCollectionIdentifier"
    
    private var dataSource:Array = [HomeRecommandRecord?]()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        
        
        let recommandCollectionLayout = UICollectionViewFlowLayout.init()
        recommandCollectionLayout.scrollDirection = .horizontal
        
        self.recommandCollectionView.dataSource = self
        self.recommandCollectionView.delegate = self
        self.recommandCollectionView.setCollectionViewLayout(recommandCollectionLayout, animated: true)
        
        self.recommandCollectionView.register(UINib.init(nibName: "MCTrainRecommandCollectionCell", bundle: Bundle.main), forCellWithReuseIdentifier: recommandCollectionIdentifier)
        
        self.recommand(current: 1)
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

    }
    
    //推荐计划
   private func recommand(current:Int?) -> Void {
        NetWorkRequest(HomePlanApi.recommandPlan(current: current, planType: 0)) { ResponseModel in
            let recommandList = HomeRecommandInfoListModel.init(JSONString: ResponseModel.dataString)
            if current == 1 {
                self.dataSource.removeAll()
                self.dataSource.append(contentsOf:  recommandList?.records ?? [])
            }else {
                self.dataSource.append(contentsOf:  recommandList?.records ?? [])
            }
            self.recommandCollectionView.reloadData()
        } failureCallback: { ResponseModel in
            self.contentView.makeToast("网络失败",position: .center)
        }
    }
    
}

extension MCTrainRecommandCell:UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: 120, height: 102)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 10
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        return UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10)
    }
    
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataSource.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell:MCTrainRecommandCollectionCell = collectionView.dequeueReusableCell(withReuseIdentifier: recommandCollectionIdentifier, for: indexPath) as! MCTrainRecommandCollectionCell
        
        let recommandModel = self.dataSource[indexPath.row]
        
        cell.headerImageView.kf.setImage(with: URL(string: recommandModel?.planUrl ?? ""))
        cell.titleInfoLabel.text = recommandModel?.planName ?? ""
        return cell
    }
    
}
